#include "GameScreen.h"

#include "GraphTypes.h"
#include "Character.h"
#include "ClippingPlane.h"
#include "Entity Manager.h"
#include "Message Dispatcher.h"
#include "LoadingScreen.h"
#include "ExecutionLog.h"
#include "GameWorld.h"
#include "GLFT_Font.hpp"

#include "Game Factory.h"
#include "Globals.hpp"
#include "EventHandler.h"

GameScreen::GameScreen()						
{
	// Create the UI
	Lua->RunScript("Scripts\\UI\\DisableAll.lua");
	GUI->RegisterPanel(Key->GetPanelIDs()->Game_Unit);
	GUI->ActivatePanel();

	string mapName = DataLoader->GetMapName();
	string display = "(";
	display += mapName;
	display += ")";
	
  LoadingScreen loadScreen("Game Screen", display, "Setting Variables");

	loadScreen.SetProgress(.05);
	loadScreen.Render();
	// These statements deteremine how often a section of code is ran.
	// It scales with the speed of the computer.
	m_TIME_CRITICAL = true;
	m_HIGH_PRIORITY = true;
	m_MEDIUM_PRIORITY = true;
	m_LOW_PRIORITY = true;
	m_HIGH_PRIO_I = m_MEDIUM_PRIO_I = m_LOW_PRIO_I = 0;

	
	loadScreen.SetProgress(.1, "Creating the grid");
	loadScreen.Render();

	// NEW LOADING SECTION
	for(double i = 0.1; i < .2; i += .004)
	{
		loadScreen.SetProgress(i, "Generating a new map");
		loadScreen.Render();
	}
	Lua->RunScript(DataLoader->GetMapScriptPath().c_str());
	
	World->LoadMap(DataLoader->GetMapPath());
	World->GetPoolManager().CreatePools(WorldMap->GetGrid());
	
	// NEW LOADING SECTION
	for(double i = 0.2; i < .35; i += .004)
	{
		loadScreen.SetProgress(i, "Loading in the map");
		loadScreen.Render();
	}

	
	// NEW LOADING SECTION
	for(double i = 0.35; i < .5; i += .006)
	{
		loadScreen.SetProgress(i, "Creating minimap");
		loadScreen.Render();
	}
//	minimap.Init(DataLoader->GetMapPath().c_str());
//	minimap.SetLocation(10, DataLoader->WINDOW_HEIGHT - DataLoader->GetMapHeight() - 20 - 10);

	// NEW LOADING SECTION
	for(double i = 0.5; i < .7; i += .004)
	{
		loadScreen.SetProgress(i, "Creating entities");
		loadScreen.Render();
	}


	Character* aMage = Factory->CreateCharacter(MageID, Vector2D(DataLoader->GetMapWidth() + 200, DataLoader->GetMapHeight() + 250));
			
	World->AddEntity(aMage);
	
	
	
	for(float i = 0.7; i < 1; i += .009)
	{
		loadScreen.SetProgress(i, "Finalizing game data");
		loadScreen.Render();
	}


	// Start the timers
#ifdef VISUAL_DEBUG
	fpsCounter.Restart();
	fpsUpdateCounter.Restart();
	fpsFrame = 0;
#endif


	// NEW LOADING SECTION
	loadScreen.SetProgress(.85, "Creating User Interface...");
	loadScreen.Render();

	World->Initialize();

}

GameScreen::~GameScreen()
{
}

void GameScreen::handle_events()
{
	while(SDL_PollEvent(&event))
	{
		EventHandler::Instance()->HandleEvents(event);
	}
}

void GameScreen::logic()
{	
	Coords coords;
	Vector2D vec;
	int id;

	World->Update(Entity::GameTimer.GetElapsedSeconds());

	Entity::GameTimer.Restart();


	// These statements deteremine how often a section of code is ran.
	// It scales with the speed of the computer.
	++m_HIGH_PRIO_I;
	++m_MEDIUM_PRIO_I;
	++m_LOW_PRIO_I;

	if(m_HIGH_PRIO_I > 1)
	{
		m_HIGH_PRIORITY = true;
		m_HIGH_PRIO_I = 0;
	}
	else
		m_HIGH_PRIORITY = false;

	if(m_MEDIUM_PRIO_I > 3)
	{
		m_MEDIUM_PRIORITY = true;
		m_MEDIUM_PRIO_I = 0;
	}
	else
		m_MEDIUM_PRIORITY = false;

	if(m_LOW_PRIO_I > 5)
	{
		m_LOW_PRIORITY = true;
		m_LOW_PRIO_I = 0;
	}
	else
		m_LOW_PRIORITY = false;


}


void GameScreen::render()
{

	World->Render();
	Log->Display();

	// Render Objects with Alpha components

// This draws the debug panel
#ifdef VISUAL_DEBUG
	DrawPrimitives->DrawRectangle(10, 5, 240, 80, GetFixedColor(0.0f, 0.4f, 0.4f, 0.8f));

	// FPS
	Key->GetFont("onuava")->drawText(20, 10, "FPS: ", orange);
	Key->GetFont("onuava")->drawText(100, 10, fpsCaption, gray);
	
	// Mouse position
	Key->GetFont("onuava")->drawText(20, 35, "MOUSE: ", orange);
	Key->GetFont("onuava")->drawText(100, 35, GetString(World->GameMousePos().x), gray);
	Key->GetFont("onuava")->drawText(170, 35, GetString(World->GameMousePos().y), gray);

	if(fpsCounter.GetElapsedMilliseconds() > 150)
	{	
		fpsCaption = GetString(fpsFrame / (fpsCounter.GetElapsedSeconds()));
		fpsCounter.Restart();
		fpsFrame = 0;
	}

	fpsFrame++;
#endif

}
